package com.shine.minepressure.view.pop;

import android.app.DatePickerDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.shine.minepressure.R;
import com.shine.minepressure.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class HistoryDataPopup extends BottomPopupView implements View.OnClickListener {
    int mType;//1:HistoryDataChild1Fragment; 2: HistoryDataChild2Fragment ...
    OnConfirmListener confirmListener;
    OnCancelListener cancelListener;
    List<String> mSensorMessageList = new ArrayList<>();
    private String mSensorType;
    private TextView sensorMessageText;
    private Integer mSensorTypePosition;
    private TextView timeStartText, timeEndText;
    private String mTimeStart, mTimeEnd;

    public interface OnPopClickListener {
        void onConfirmClick(Integer sensorTypePosition, String mSensorType, String timeStart, String timeEnd);
    }

    private OnPopClickListener onPopClickListener;

    public HistoryDataPopup(int type, @NonNull Context context, List<String> sensorMessageList,
                            String sensorType, String timeStart, String timeEnd,
                            Integer sensorTypePosition) {
        super(context);
        mType = type;
        mSensorMessageList.addAll(sensorMessageList);

        mSensorType = sensorType;
        mTimeStart = timeStart;
        mTimeEnd = timeEnd;
        mSensorTypePosition = sensorTypePosition;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_history_data;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        TextView typeNameText = (TextView) findViewById(R.id.typeNameText);

        if (mType == 1) {
            typeNameText.setText("传感器类别");
        }
        if (mType == 2) {
            typeNameText.setText("分站名称");
        }

        ImageView cancel = (ImageView) findViewById(R.id.cancel);
        TextView confirm = (TextView) findViewById(R.id.confirm);

        sensorMessageText = (TextView) findViewById(R.id.sensorMessageText);
        timeStartText = (TextView) findViewById(R.id.timeStart);
        timeEndText = (TextView) findViewById(R.id.timeEnd);

        if (!TextUtils.isEmpty(mSensorType))
            sensorMessageText.setText(mSensorType);

        if (!TextUtils.isEmpty(mTimeStart))
            timeStartText.setText(mTimeStart);

        if (!TextUtils.isEmpty(mTimeEnd))
            timeEndText.setText(mTimeEnd);

        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);
        timeStartText.setOnClickListener(this);
        timeEndText.setOnClickListener(this);

        findViewById(R.id.sensorMessageLayout).setOnClickListener(this);

    }

    public HistoryDataPopup setListener(OnConfirmListener confirmListener, OnCancelListener onCancelListener, OnPopClickListener onPopClickListener) {
        this.confirmListener = confirmListener;
        this.cancelListener = onCancelListener;
        this.onPopClickListener = onPopClickListener;
        return this;
    }

    private void setSensorMessage(List<String> sensorMessageList) {
        CommonListBottomPopup commonListBottomPopup1 = new CommonListBottomPopup(getContext(), sensorMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup1)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup1.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mSensorType = sensorMessageList.get(position);
                mSensorTypePosition = position;
                sensorMessageText.setText(mSensorType);
                commonListBottomPopup1.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                if (confirmListener != null) confirmListener.onConfirm();


                if (sensorMessageText.getText().toString().isEmpty()) {
                    if (mType == 1) {
                        ToastUtils.showShort("请选择传感器类别");
                    }
                    if (mType == 2) {
                        ToastUtils.showShort("请选择分站名称");
                    }
                    return;
                }
                if (timeEndText.getText().toString().equals("开始时间") || timeStartText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择开始时间");
                    return;
                }
                if (timeEndText.getText().toString().equals("结束时间") || timeEndText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择结束时间");
                    return;
                }
                onPopClickListener.onConfirmClick(mSensorTypePosition,
                        sensorMessageText.getText().toString(),
                        timeStartText.getText().toString(), timeEndText.getText().toString());
                dismiss();
                break;
            case R.id.cancel:
                if (cancelListener != null) cancelListener.onCancel();
                dismiss();
                break;
            case R.id.timeStart:
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        timeStartText.setText(year + "/" + (month + 1) + "/" + dayOfMonth + " " + "00:00:00");
//                        new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
//                            @Override
//                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                                timeStart.setText(year + "-" + (month + 1) + "-" + dayOfMonth + " " + hourOfDay + ":" + minute);
//                            }
//                        }, 0, 0, true).show();

                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.timeEnd:
                Calendar calendar3 = Calendar.getInstance();
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        timeEndText.setText(year + "/" + (month + 1) + "/" + dayOfMonth + " " + "23:59:59");
//                        new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
//                            @Override
//                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                                timeStart.setText(year + "-" + (month + 1) + "-" + dayOfMonth + " " + hourOfDay + ":" + minute);
//                            }
//                        }, 0, 0, true).show();

                    }
                }, calendar3.get(Calendar.YEAR), calendar3.get(Calendar.MONTH), calendar3.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.sensorMessageLayout:
                setSensorMessage(mSensorMessageList);
                break;
            default:
                break;
        }
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
//        return XPopupUtils.getWindowHeight(getContext());
        return (int) (XPopupUtils.getWindowHeight(getContext()) * .9f);
    }

}
